package aplicacao;

import escalonamento.EscalonamentoFCFS;
import escalonamento.EscalonamentoPP;
import escalonamento.EscalonamentoRR;
import escalonamento.EscalonamentoSRT;
import escalonamento.EscalonamentoSRTP;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import model.Arquivo;
import model.Processo;

/**
 *
 * @author João Gonçalves
 * @author Everton Alves
 */
public class PoliticasEscalonamentos extends javax.swing.JFrame {

    private ArrayList<Processo> processos;
    private String dadosProcessamento;

    public PoliticasEscalonamentos() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    private void escalonamentos(String caminhoArquivo, int indiceEscalonamento) {
        try {

            processos = Arquivo.carregarArquivo(caminhoArquivo);

            try {
                switch (indiceEscalonamento) {
                    case 0:
                        EscalonamentoFCFS escalonamentoFCFS = new EscalonamentoFCFS();
                        dadosProcessamento = escalonamentoFCFS.escalonamento(processos);

                        break;
                    case 1:
                        EscalonamentoRR escalonamentoRR = new EscalonamentoRR();
                        dadosProcessamento = escalonamentoRR.escalonamento(processos);

                        break;
                    case 2:
                        EscalonamentoPP escalonamentoPP = new EscalonamentoPP();
                        dadosProcessamento = escalonamentoPP.escalonamento(processos);

                        break;
                    case 3:
                        EscalonamentoSRT escalonamentoSRT = new EscalonamentoSRT();
                        dadosProcessamento = escalonamentoSRT.escalonamento(processos);

                        break;
                    case 4:
                        EscalonamentoSRTP escalonamentoSRTP = new EscalonamentoSRTP();
                        dadosProcessamento = escalonamentoSRTP.escalonamento(processos);

                        break;
                    default:
                        textProcessamento.setText("Escalomento não escolhido!");
                        break;
                }

                System.out.println(dadosProcessamento + "\n\n");

            } catch (ArrayIndexOutOfBoundsException aiobe) {

                String avisoErro = "Caminho do arquivo inválido! \n" + aiobe + Integer.parseInt(aiobe.getMessage()) + 1;
                textProcessamento.setText(avisoErro);
                System.out.println(avisoErro);
            }
        } catch (FileNotFoundException fnfe) {
            textProcessamento.setText(fnfe.toString());
            System.out.println(fnfe);
        }
    }

    /**
     * Metódo responsavel por capturar o caminho do arquivo selecionado.
     */
    public void selecaoArquivo() {
        int result = jFileChooserSelecionarArquivo.showOpenDialog(null);

        if (result == JFileChooser.APPROVE_OPTION) {
            textArquivo.setText(jFileChooserSelecionarArquivo.getSelectedFile().getAbsolutePath());
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooserSelecionarArquivo = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        textArquivo = new javax.swing.JTextField();
        buttonSelecionarArquivo = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        comboPolitica = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textProcessamento = new javax.swing.JTextArea();
        buttonConfirmar = new javax.swing.JButton();

        jFileChooserSelecionarArquivo.setApproveButtonText("Selecionar");
        jFileChooserSelecionarArquivo.setDialogTitle("Selecione processos de entrada");
        jFileChooserSelecionarArquivo.setFileFilter(new FileNameExtensionFilter("Documentos de texto (*.txt)", "txt"));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Politicas de Escalonamento");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Escolha do arquivo"));

        jLabel1.setText("Arquivo:");

        textArquivo.setText("arquivo.txt");

        buttonSelecionarArquivo.setText("Selecionar");
        buttonSelecionarArquivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSelecionarArquivoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(textArquivo, javax.swing.GroupLayout.PREFERRED_SIZE, 478, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonSelecionarArquivo)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textArquivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(buttonSelecionarArquivo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Politica "));

        comboPolitica.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "FCFS (First-Come, First Served)", "RR (Round-Robin)", "PP (Prioridade Preemptivo)", "Custom (SRT)", "SRTP (SRT com Prioridade em tempos iguais)" }));
        comboPolitica.setSelectedItem(null);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboPolitica, 0, 512, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(comboPolitica, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 11, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Processamento"));

        textProcessamento.setColumns(20);
        textProcessamento.setRows(5);
        jScrollPane1.setViewportView(textProcessamento);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE)
                .addContainerGap())
        );

        buttonConfirmar.setText("Confirmar");
        buttonConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonConfirmarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 1, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(buttonConfirmar)
                        .addGap(31, 31, 31)))
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonConfirmarActionPerformed
        escalonamentos(textArquivo.getText(), comboPolitica.getSelectedIndex());
        textProcessamento.setText(dadosProcessamento);
    }//GEN-LAST:event_buttonConfirmarActionPerformed

    private void buttonSelecionarArquivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSelecionarArquivoActionPerformed
        selecaoArquivo();
    }//GEN-LAST:event_buttonSelecionarArquivoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(final String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                } else if ("GKT".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PoliticasEscalonamentos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PoliticasEscalonamentos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PoliticasEscalonamentos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PoliticasEscalonamentos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                PoliticasEscalonamentos politicasEscalonamentos = new PoliticasEscalonamentos();

                if (args.length > 0) {

                    switch (args[1]) {
                        case "FCFS":
                            politicasEscalonamentos.escalonamentos(args[0], 0);
                            break;
                        case "RR":
                            politicasEscalonamentos.escalonamentos(args[0], 1);
                            break;
                        case "PP":
                            politicasEscalonamentos.escalonamentos(args[0], 2);
                            break;
                        case "Custom":
                            politicasEscalonamentos.escalonamentos(args[0], 3);
                            break;
                        case "SRTP":
                            politicasEscalonamentos.escalonamentos(args[0], 4);
                            break;
                        default:
                            System.out.println("Escalonamento inválido!");
                    }

                    politicasEscalonamentos.dispose();

                } else {
                    politicasEscalonamentos.setVisible(true);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonConfirmar;
    private javax.swing.JButton buttonSelecionarArquivo;
    private javax.swing.JComboBox comboPolitica;
    private javax.swing.JFileChooser jFileChooserSelecionarArquivo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField textArquivo;
    private javax.swing.JTextArea textProcessamento;
    // End of variables declaration//GEN-END:variables
}
